package com.beyond.trace.dy.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.beyond.trace.dy.domain.TraceDyFarmers;
import com.beyond.trace.dy.service.ITraceDyFarmersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 养殖户管理Controller
 * 
 * @author Mr.Yao
 * @date 2022-04-08
 */
@RestController
@RequestMapping("/dy/farmers")
@Api(value = "养殖户管理", tags = "养殖户管理")
public class TraceDyFarmersController extends BaseController
{
    @Autowired
    private ITraceDyFarmersService traceDyFarmersService;

    //@PreAuthorize("@ss.hasPermi('dy:farmers:list')")
    @GetMapping("/list")
    @ApiOperation("查询养殖户管理列表")
    public TableDataInfo list(TraceDyFarmers traceDyFarmers)
    {
        startPage();
        List<TraceDyFarmers> list = traceDyFarmersService.selectTraceDyFarmersList(traceDyFarmers);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('dy:farmers:query')")
    @GetMapping(value = "/query")
    @ApiOperation("查询养殖户管理列表不分页")
    public AjaxResult getInfoQuery(TraceDyFarmers traceDyFarmers)
    {
        return AjaxResult.success(traceDyFarmersService.selectTraceDyFarmersList(traceDyFarmers));
    }

    //@PreAuthorize("@ss.hasPermi('dy:farmers:export')")
    @Log(title = "养殖户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TraceDyFarmers traceDyFarmers)
    {
        List<TraceDyFarmers> list = traceDyFarmersService.selectTraceDyFarmersList(traceDyFarmers);
        ExcelUtil<TraceDyFarmers> util = new ExcelUtil<TraceDyFarmers>(TraceDyFarmers.class);
        return util.exportExcel(list, "养殖户管理数据");
    }

    //@PreAuthorize("@ss.hasPermi('dy:farmers:query')")
    @GetMapping(value = "/{farmersId}")
    @ApiOperation("查询养殖户管理列表")
    public AjaxResult getInfo(@PathVariable("farmersId") Long farmersId)
    {
        return AjaxResult.success(traceDyFarmersService.selectTraceDyFarmersById(farmersId));
    }

    //@PreAuthorize("@ss.hasPermi('dy:farmers:add')")
    @Log(title = "养殖户管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增养殖户管理")
    public AjaxResult add(@RequestBody TraceDyFarmers traceDyFarmers)
    {
        return toAjax(traceDyFarmersService.insertTraceDyFarmers(traceDyFarmers));
    }

    //@PreAuthorize("@ss.hasPermi('dy:farmers:edit')")
    @Log(title = "养殖户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改养殖户管理")
    public AjaxResult edit(@RequestBody TraceDyFarmers traceDyFarmers)
    {
        return toAjax(traceDyFarmersService.updateTraceDyFarmers(traceDyFarmers));
    }

    //@PreAuthorize("@ss.hasPermi('dy:farmers:remove')")
    @Log(title = "养殖户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{farmersIds}")
    @ApiOperation("删除养殖户管理")
    public AjaxResult remove(@PathVariable Long[] farmersIds)
    {
        return toAjax(traceDyFarmersService.deleteTraceDyFarmersByIds(farmersIds));
    }

    @Log(title = "图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/picture/upload")
    @ApiOperation("图片上传接口")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            if (!file.isEmpty()) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/dy/farmers", file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("path", avatar);
                return ajax;
            } else {
                return AjaxResult.error("上传图片异常，请联系管理员");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
